package com.glasses.business.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.glasses.business.R;
import com.glasses.business.appliction.MyApplication;
import com.glasses.business.base.AppBaseFragment;
import com.glasses.business.entity.PayInfo;
import com.glasses.business.event.BussEvent;
import com.glasses.business.pay.alipay.AlipayHelper;
import com.glasses.business.pay.com.PayAgent;
import com.glasses.business.pay.com.PayAgentUtils;
import com.glasses.business.pay.com.PayListener;
import com.glasses.business.pay.wxpay.WechatPayHelper;

import org.haitao.common.utils.AppLog;
import org.haitao.common.utils.ToastUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * Author：邓勇军
 * English Name：Deng,The Sky Fucker!
 * Email：dandycoder@126.com
 * Date：2017/3/9
 * Version：1.0
 */

public class PayFragment extends AppBaseFragment {
    @InjectView(R.id.wxSelect)
    ImageView wxSelect;
    @InjectView(R.id.wxRv)
    RelativeLayout wxRv;
    @InjectView(R.id.aliPaySelect)
    ImageView aliPaySelect;
    @InjectView(R.id.aliPayRv)
    RelativeLayout aliPayRv;
    @InjectView(R.id.payMoney)
    TextView payMoney;
    @InjectView(R.id.payTime)
    TextView payTime;
    @InjectView(R.id.payBuy)
    TextView payBuy;
    private PayInfo moneyPay;
    private int payType = 1;

    @Override
    public int getRootViewId() {
        return R.layout.fragment_pay;
    }

    @Override
    public void setViews() {
        setTitleTv("收银台");
        if (getArguments() != null) {
            moneyPay = ((PayInfo) getArguments().getSerializable("payMoney"));
            switch (moneyPay.getIsUploadCheckMenu()) {
                case 0:
                    payMoney.setText("实际价格:￥" + moneyPay.getFinalPrice());
                    break;
                case 1:
                    payMoney.setText("实际价格:￥" + moneyPay.getTotalPrice() + "-26=" + moneyPay.getFinalPrice());
                    break;
            }
        }
//                new AlertDialog.Builder(getActivity()).setMessage("确认离开收银台？")
//                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialogInterface, int i) {
//                                dialogInterface.dismiss();
//                            }
//                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialogInterface, int i) {
//                        dialogInterface.dismiss();
//
//                    }
//                }).create().show();
//            }
    }

    @Override
    public void initData() {

    }

    @Override
    public boolean mainTabShow() {
        return false;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.inject(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @OnClick({R.id.wxRv, R.id.aliPayRv, R.id.payBuy})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.wxRv:
                payType = 1;
                wxSelect.setImageResource(R.mipmap.icon_xz2);
                aliPaySelect.setImageResource(R.mipmap.icon_xz_copy);
                break;
            case R.id.aliPayRv:
                payType = 2;
                wxSelect.setImageResource(R.mipmap.icon_xz_copy);
                aliPaySelect.setImageResource(R.mipmap.icon_xz2);
                break;
            case R.id.payBuy:
                switch (payType) {
                    case 1:
                        EventBus.getDefault().post(new BussEvent(BussEvent.After_Pay));
                        MyApplication.getInstance().manager.onBackPressed();
                        PayAgentUtils.getInstance().pay(getActivity(), PayAgent.PayType.WECHATPAY, moneyPay, new PayListener() {
                            @Override
                            public void onPaySuccess(PayAgent.PayType payType) {
                                AppLog.e("==========onPaySuccess=============");
                            }

                            @Override
                            public void onPayFail(PayAgent.PayType payType, int code, String msg) {
                                AppLog.e("==========onPayFail=============");
                            }
                        });
                        break;
                    case 2:
                        PayAgentUtils.getInstance().pay(getActivity(), PayAgent.PayType.ALIPAY, moneyPay, new PayListener() {
                            @Override
                            public void onPaySuccess(PayAgent.PayType payType) {
                                EventBus.getDefault().post(new BussEvent(BussEvent.After_Pay));
                                MyApplication.getInstance().manager.onBackPressed();
                            }

                            @Override
                            public void onPayFail(PayAgent.PayType payType, int code, String msg) {
                            }
                        });
                        break;
                    default:
                        ToastUtil.shortShowToast("请先选择支付类型...");
                        break;
                }
                break;
        }
    }
}
